package com.biz.primus.ms.commodity.service.frontend;

import com.biz.primus.base.constant.Constant;
import com.biz.primus.base.enums.SaleChannel;
import com.biz.primus.base.vo.commodity.SaleStatus;
import com.biz.primus.commodity.enums.GbStatus;
import com.biz.primus.commodity.enums.GoodsKind;
import com.biz.primus.commodity.vo.SyncCommodityVo;
import com.biz.primus.commodity.vo.backend.CommodityUpdateVo;
import com.biz.primus.commodity.vo.backend.PlatformCommodityDetailVo;
import com.biz.primus.common.utils.CopyUtils;
import com.biz.primus.ms.base.service.AbstractBaseService;
import com.biz.primus.ms.commodity.dao.po.Product;
import com.biz.primus.ms.commodity.dao.po.RelevanceProduct;
import com.biz.primus.ms.commodity.dao.redis.ProductRedisDao;
import com.biz.primus.ms.commodity.dao.repository.brand.BrandRepository;
import com.biz.primus.ms.commodity.dao.repository.category.CategoryRepository;
import com.biz.primus.ms.commodity.dao.repository.product.ProductRepository;
import com.biz.primus.ms.commodity.dao.repository.product.RelevanceProductRepository;
import com.biz.primus.ms.commodity.dao.ro.ProductRo;
import com.biz.primus.ms.commodity.trans.Product2ProductRo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Slf4j
public abstract class AbstractCommodityService extends AbstractBaseService {

    @Autowired
    protected ProductRepository productRepository;

    @Autowired
    protected RelevanceProductRepository relevanceProductRepository;

    @Autowired
    protected ProductRedisDao productRedisDao;

    @Autowired
    protected BrandRepository brandRepository;

    @Autowired
    protected CategoryRepository categoryRepository;


    protected void saveProduct(Product product) {
        productRepository.save(product);
        ProductRo productRo = new Product2ProductRo(brandRepository, categoryRepository).apply(product);
        delayer.executeAfterTransactionCommit(()->{
            productRedisDao.save(productRo);
            productRedisDao.appendProductCode2TypeProductSortedSet(product.getProductCode(),product.getProductType());
        });
    }

    protected void saveProducts(List<Product> productList) {
        for (Product product : productList) {
            this.saveProduct(product);
        }
    }

    protected PlatformCommodityDetailVo getPlatformCommodityDetailVo(Product product) {
        PlatformCommodityDetailVo respVo = new PlatformCommodityDetailVo();
        CopyUtils.copyProperties(product, respVo);
        respVo.setSaleTagIds(product.getSaleTagIdList());
        respVo.setExtendProductVos(product.getExtendProducts());
        return respVo;
    }

    protected void platformUpdateCommodity(Product product, CommodityUpdateVo reqVo) {
        product.setShowStock(reqVo.getShowStock());
        product.setShowRelevant(product.getShowRelevant());
        product.setSeoKeywords(reqVo.getSeoKeywords());
        product.setPointsProportion(reqVo.getPointsProportion());
        product.setOnSaleEffectTime(reqVo.getOnSaleEffectTime());
        product.setOffSaleEffectTime(reqVo.getOffSaleEffectTime());
        product.setLogo(reqVo.getLogo());
        product.setImages(reqVo.getImages());
        product.setIntroImages(reqVo.getIntroImages());
        product.setSaleChannels(reqVo.getSaleChannel());
        product.setGlobalIdx(reqVo.getGlobalIdx());
        if (CollectionUtils.isNotEmpty(reqVo.getSaleChannel())) {
            if (reqVo.getSaleChannel().contains(SaleChannel.ALL)) {
                product.setAppSaleStatus(SaleStatus.ON_SALE);
                product.setWebSaleStatus(SaleStatus.ON_SALE);
            } else {
                if (reqVo.getSaleChannel().contains(SaleChannel.WEB)) {
                    product.setWebSaleStatus(SaleStatus.ON_SALE);
                } else {
                    product.setWebSaleStatus(SaleStatus.OFF_SALE);
                }
                if (reqVo.getSaleChannel().contains(SaleChannel.APP)) {
                    product.setAppSaleStatus(SaleStatus.ON_SALE);
                } else {
                    product.setAppSaleStatus(SaleStatus.OFF_SALE);
                }
            }
        }
        product.setSaleStatus(reqVo.getSaleStatus());
        updateRelevance(product, reqVo.getRelevanceProductIds());
    }

    protected void updateCommodity(SyncCommodityVo reqVo, Product product) {
        CopyUtils.copyProperties(reqVo, product);
        product.setGoodsKind(GoodsKind.valueOf(reqVo.getGoodsKind()));
        product.setGbStatus(GbStatus.valueOf(reqVo.getGbStatus()));
    }

    private void updateRelevance(Product product, List<Long> relevanceProductIds) {
        List<Product> relevanceProducts = productRepository.findByIdIn(relevanceProductIds);
        if (CollectionUtils.isNotEmpty(relevanceProductIds)) {
            if (product.getRelevanceProduct() == null) {
                //无关联关系 设置关联关系
                RelevanceProduct relevanceProduct = new RelevanceProduct();
                relevanceProduct.setId(idService.getNextId());
                relevanceProduct.setProducts(relevanceProducts);
                relevanceProducts.forEach((Product p) -> p.setRelevanceProduct(relevanceProduct));
                relevanceProductRepository.save(relevanceProduct);
            } else {
                //有关联关系  清空旧关系  设置新关系
                RelevanceProduct relevanceProduct = product.getRelevanceProduct();
                relevanceProduct.getProducts().forEach((Product p)->p.setRelevanceProduct(null));
                relevanceProductRepository.save(relevanceProduct);
                relevanceProducts.forEach((Product p) -> p.setRelevanceProduct(relevanceProduct));
                relevanceProduct.setProducts(relevanceProducts);
                relevanceProductRepository.save(relevanceProduct);
            }
        }
    }

    protected ProductRo getProductRo(String productCode) {
        ProductRo productRo = productRedisDao.findOne(productCode);
        if (productRo == null) {
            Product product = productRepository.findByProductCode(productCode);
            if (product == null) {
                productRedisDao.createTempProduct(productCode, Constant.TEMP_PRODUCT_EXPIRE_SEC);
            }
            productRo = new Product2ProductRo(brandRepository,categoryRepository).apply(product);
            if (product!=null){
                productRedisDao.save(productRo);
                productRedisDao.persist(productRedisDao.getHashKey(productCode));
            }
        }
        return productRo;
    }

}
